/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    Sep 2024
Written on Stata 17 MP / MacOS 15.0 (24A335)
References:          
==================================================*/

/* This is a master Do file */

/*

Running this file will replicate all the figures and tables in the paper and the online appendix.

Some advice:
        1. Although you can run the whole code in one go, we recommend that you run it
        exhibit by exhibit, as it may take a long time.
        2. This code includes some time-consuming calculations; depending on your computing 
        power, it may take a while. 
        3. Make sure to change all the paths where relevant in the program set section.
        4. Make sure you keep the folder structure of the replication package when running 
        to avoid errors.
        5. Although maintaining the do-files order is not strictly necessary, it is strongly 
        recommended, as some codes use results from other "previous" ones.
*/

/*==================================================
            0: Program set up
==================================================*/
drop _all
*set varabbrev off	// no variable abbreviations allowed (personal preference)

// Additional packages
*ssc install gtools
*net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
*set scheme cleanplots, perm
*ssc install icw_index
*ssc install outreg2
*ssc install winsor2
*ssc install binscatter

// Folder root
clear all
if "`c(username)'" == "davidvargas"			global root "/Users/davidvargas/Dropbox/Research/BID/Targeting_project"
else if inlist("`c(username)'", "David" , "DAVIDVAR" )	global root "C:\Users\\`c(username)'\Dropbox\Research\BID\Targeting_project"
else 							global root "D:\Dropbox"

// Paths
global dir1r "$root/11_replication"
global dir2r "$dir1r/02_Data/02_Rawdata"
global dir3r "$dir1r/02_Data/03_Cleandata"
global dir4r "$dir1r/03_Output/01_Tables"
global dir5r "$dir1r/03_Output/02_Graphs"
global dir6r "$dir1r/01_Codes"
global dir7r "$dir1r/02_Data/01_Codes"
*global dir8r "/Volumes/Targeting_01_encrypted" // encrypted files root


/*==================================================
        1. Perform general calculations
==================================================*/

/* These do files do some general preparation and run a set of 
calculations that are later used to generate some of the exhibits.
Please run this first to generate all neded auxiliary data */

*----------  1.1 Data 3: Aux datset with model estimates
do "${dir6r}/E001_Create_Regimes.do" 

*----------  1.1 Data 4: Aux datset with budget model estimates 
do "${dir6r}/E002_Budget_estimates.do" 


/*==================================================
        2. Generate exhibits
==================================================*/
 
/* exhibits are created in the same order as they appear on the paper */

*----------  2.1 Table 1: Incidence of Shocks

do "${dir6r}/T1_shock_incidence.do" 

*----------  2.2 Figure 1: Exposure to Economic Shocks by Distance to Extreme Poverty 

do "${dir6r}/F1_pr_shock_inc.do" 

*----------  2.2 Figure 2: Income and Poverty Dynamics by Exposure to Shocks During 2020 

do "${dir6r}/F2_dynamics.do" 

*----------  2.3 Table 2: Effects of Shocks on Income, Extreme Poverty, and Hunger

do "${dir6r}/T2_Income_dynamicsDD.do" 

*----------  2.4 Table 3: Targeting Performance by Targeting Method

do "${dir6r}/T3_Main.do" 

*----------  2.5 Figure 3: Targeting Errors, Transfer Size, and Welfare over Time under Alternative Regimes

do "${dir6r}/F3_Incl_excl_bts.do" 

*----------  2.6 Figure 4: Extreme Poverty Rates by Predicted Pre-Crisis Poverty Status (Benchmark Model)

do "${dir6r}/F4_pr_epov.do" 

*----------  2.7 Figure 5: Targeting Performance with Moral Hazard by Per-Capita Income Quintiles (2020)

do "${dir6r}/F5_mh_fig_incq.do" 
/* Note: Due to the way stata manages data loading code may stop here, 
if that happens just re-run the F5_mh_fig_incq do file. */

*----------  2.8 Figure 6:  Targeting Errors, Transfer Size, and Welfare over Time Using Aggregated Data (Sectors)

do "${dir6r}/F6_model_using_labour_pred.do"  // this stops
/* Note: Due to the way stata manages data loading code may stop here, 
if that happens just re-run the F6_model_using_labour_pred do file. */

*----------  2.9 Figure 7: Social Welfare and Fiscal Budget under Alternative Regimes

do "${dir6r}/F7_budget_expansion.do" 

*----------  2.10 Figure 8: Social Welfare Over Time for Different Coverage Rates by Targeting Tool and Coverage Rate

do "${dir6r}/F8_A8_fixed_coverage.do" 



//================ Appendix ========================//


/*----------------------------------------------------------------------
	-------------------- ADMIN DATA 	-----------------------
------------------------------------------------------------------------
 ***** Admin data access is restricted *******
*----------  2.11 Table A1: Summary Statistics: Administrative Records

do "${dir6r}/TA1_Summary Stats.do" 

----------------------------------------------------------------------
	-------------------- ADMIN DATA END 	-----------------------
------------------------------------------------------------------------*/

*----------  2.12 Table A2: Employment Status by Different Surveys and Year

do "${dir6r}/TA2_GEIH_comparisons.do" 

*----------  2.13 Table A3: Baseline PMT Regression

do "${dir6r}/TA3_Baseline_PMT.do" 

*----------  2.14 Figure A1: Exclusion and Inclusion Errors: Baseline PMT Approach

do "${dir6r}/FA1_ROC_curve.do"

*----------  2.15 Table A4: Predictive Models for Changes in Income: Income - Employment Co-movements

do "${dir6r}/TA4_Dynamic_workonly.do" 

*----------  2.16 Figure A2: Robustness: Targeting Errors, Transfer Size, and Welfare over Time under Alternative Dynamic Targeting Models 

do "${dir6r}/FA2_sensitivity_model.do" 

*----------  2.17 Figure A3: Robustness: Targeting Errors, Transfer Size, and Welfare over Time Using LASSO to Estimate the PMT Model

do "${dir6r}/FA3_Incl_excl_bts_lasso.do" 

*----------  2.15 Figure A4: Targeting Errors, Transfer Size, and Welfare over Time Using Updated Information on a PMT Cross-Sectional model.

do "${dir6r}/FA4_Incl_excl_pmt_updates.do" 

*----------  2.16 Figure A5: Robustness: Targeting Errors, Transfer Size, and Welfare over Time Correcting for Moral Hazard in Reported Employment

do "${dir6r}/FA5_moralhazard.do" 

*----------  2.16 Figure A6: Targeting Errors, Transfer Size, and Welfare over Time Using Aggregated Data (Region)

do "${dir6r}/FA6_model_using_labour_pred_dept_urb.do" 
/* Note: Due to the way stata manages data loading code may stop here, 
if that happens just re-run the FA6_model_using_labour_pred_dept_urb do file. */

*----------  2.16 Figure A7: Targeting Errors, Transfer Size, and Welfare over Time under Dynamic Targeting Including Negative Non-labor Shocks

do "${dir6r}/FA7_adding_shocks.do" 

*----------  2.17 Figure A8: Exclusion and Inclusion Errors by Different Coverage Rates and Targeting Tools
/* this is done in 2.10 */

